<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;

use app\common\traits\Page;
use think\Db;
use app\admin\model\Employee;
use nx\Data;
/**
 * Description of Staff
 *
 * @author HuangLuSen
 */
class Yuangong extends Admin {
    protected $user_model;
    use Page;
    public function _initialize()
    {
        parent::_initialize();
        $this->user_model = new Employee();
    }

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index()
    {
        $keyword = input('keyword');
        $bumen = input('bumen');
        $map = [];
        if ($keyword) {
            $map['name|phone'] = ['like', "%{$keyword}%"];
        }
        if ($bumen) {
            $map['sid'] = $bumen;
        }
        $user_list = $this->pageshow('Employee',$map,'','department');
//        $user_list = $this->user_model->where($map)->with('department')->order('id desc')->paginate(50, false, ['page' => $page]);
        $depar = Db::name('department')->select();
        $depar = Data::tree($depar,'sectionname','id','fid');
        return $this->fetch('index', ['user_list' => $user_list, 'keyword' => $keyword,'depar'=>$depar,'bumen'=>$bumen]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        $depar = Db::name('department')->select();
        $depar = Data::tree($depar,'sectionname','id','fid');
        $this->assign('depar',$depar);
        return $this->fetch();
    }

    /**
     * 保存用户
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Employee');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->user_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $user = $this->user_model->find($id);
        $depar = Db::name('department')->select();
        $depar = Data::tree($depar,'sectionname','id','fid');
        return $this->fetch('edit', ['info' => $user,'depar'=>$depar]);
    }

    /**
     * 更新用户
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'Employee.edit');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $user           = $this->user_model->find($id);
                $user->id       = $id;
                $user->name = $data['name'];
                $user->phone   = $data['phone'];
                $user->sid    = $data['sid'];
                $user->status   = 1;
                if (!empty($data['password']) && !empty($data['confirm_password'])) {
                    $user->password = md5($data['password'] . Config::get('salt'));
                }
                if ($user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {
        if ($this->user_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
